package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.trkj.dao.jpa.CcHospapplyDAO;
import com.trkj.dao.jpa.IHWCTbdepartmentDAO;
import com.trkj.dao.jpa.IHwcPostDAO;
import com.trkj.dao.jpa.IHwcRoleDAO;
import com.trkj.dao.jpa.IHwcStaffDAO;
import com.trkj.dao.jpa.IHwcTitleDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcHospapply;
import com.trkj.pojos.JlExamineApply;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbpost;
import com.trkj.pojos.Tbrole;
import com.trkj.pojos.Tbstaff;
import com.trkj.pojos.Tbtitle;
import com.trkj.vo.CcHospapplyVo;
import com.trkj.vo.HwcStaffVO;

@Service
public class CcHospapplyService {
	@Autowired
	private CcHospapplyDAO cchospDAO;
	@Autowired
	private TbdepartmentDAO tbdepartDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;

	@Autowired
	private IHWCTbdepartmentDAO deptDAO;

	@Autowired
	private IHwcRoleDAO roleDAO;

	@Autowired
	private IHwcTitleDAO titleDAO;
	@Autowired
	private IHwcPostDAO postDAO;

	public Map<String, Object> queryhaname(int page, int rows, String haname) {
		PageRequest pr = new PageRequest(page - 1, rows);
		Map<String, Object> map = new HashMap<>();
		map.put("total", cchospDAO.findByHanamecount(haname));
		map.put("rows", poStuList2VoList(cchospDAO.findByHaname(pr, haname)));
		return map;
	}

	public  Map<String, Object> queryTbdepartment(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		Map<String, Object> map = new HashMap<>();
		map.put("total", deptDAO.count());
		map.put("rows", deptDAO.findTbdepartmentPage(pr));
		return map;
	}
	public List<HwcStaffVO> queryTbstaff () {
		List<Tbstaff>list=(List<Tbstaff>) staffDAO.findAll();
		List<HwcStaffVO> listVo=new ArrayList<>();
		for(Tbstaff tb:list){
			HwcStaffVO staffVO = staffTOVO(tb);
			listVo.add(staffVO);
		}
		return listVo;
	}
	public List<HwcStaffVO> fidByTbstaff(BigDecimal departmentNo) {
		List<Tbstaff>list=staffDAO.findBydepartmentNo(departmentNo);
		List<HwcStaffVO> listVo=new ArrayList<>();
		for(Tbstaff tb:list){
			HwcStaffVO staffVO = staffTOVO(tb);
			listVo.add(staffVO);
		}
		return listVo;
	}

	private HwcStaffVO staffTOVO(Tbstaff staff) {

		Tbpost post = new Tbpost();
		Tbtitle title = new Tbtitle();
		Tbrole role = new Tbrole();
		HwcStaffVO staffVO = new HwcStaffVO();
		Tbdepartment dept = new Tbdepartment();

		staffVO.setStaffNo(staff.getStaffNo());
		staffVO.setStaffName(staff.getStaffName());
		staffVO.setStaffAccount(staff.getStaffAccount());
		staffVO.setStaffAge(staff.getStaffAge());
		staffVO.setStaffIdcard(staff.getStaffIdcard());
		staffVO.setStaffPassword(staff.getStaffPassword());
		staffVO.setStaffPhone(staff.getStaffPhone());
		staffVO.setStaffSex(staff.getStaffSex());
		staffVO.setStaffState(staff.getStaffState());

		staffVO.setDepartmentNO(staff.getDepartmentNo().longValue());
		staffVO.setDepartmentName(tbdepartDAO.queryName(staffVO.getDepartmentNO()));
			return staffVO;

	}

	public void alertAsk(Long[] applyid, String hpdoctorid) {
		for (int i = 0; i < applyid.length; i++) {
			BigDecimal doctorid = new BigDecimal(hpdoctorid);
			CcHospapply cchosp = cchospDAO.findOne(applyid[i]);
			if (cchosp != null) {
				cchosp.setHpremarks("已取消");
				cchosp.setHpdoctorid(doctorid);
				cchosp.setHpdate(new Date());
				cchospDAO.save(cchosp);
			}
		}
	}
	public CcHospapply queryByOne(String applyid){
		long id = Integer.valueOf(applyid);
		return cchospDAO.findOne(id);
	}
	public void alertRequest(String applyid, String hpdoctorid) {
		long id = Integer.valueOf(applyid);
		CcHospapply cchosp = cchospDAO.findOne(id);
		if (cchosp != null) {
			cchosp.setHpremarks("已办理");
			cchosp.setHpdoctorid(new BigDecimal(hpdoctorid));
			cchosp.setHpdate(new Date());
			cchospDAO.save(cchosp);
		}
	}

	public Map<String, Object> queryHmedicalid(int page, int rows, BigDecimal hmedicalid) {
		PageRequest pr = new PageRequest(page - 1, rows);
		Map<String, Object> map = new HashMap<>();
		map.put("total", cchospDAO.findByHmedicalidcount(hmedicalid));
		map.put("rows", poStuList2VoList(cchospDAO.findByHmedicalid(pr, hmedicalid)));
		return map;
	}

	public Map<String, Object> queryAll(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		// 获取当前页的记录
		List<CcHospapply> list = cchospDAO.findByHpremarks(pr);
		// 获取总的记录条数
		Map<String, Object> map = new HashMap<>();
		map.put("total", cchospDAO.findBycount());
		map.put("rows", poStuList2VoList(list));
		return map;
	}

	public Map<String, Object> queryAllapply(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		// 获取当前页的记录
		List<CcHospapply> list = cchospDAO.findByAll(pr);
		// 获取总的记录条数
		Map<String, Object> map = new HashMap<>();
		map.put("total", cchospDAO.count());
		map.put("rows", poStuList2VoList(list));
		return map;
	}

	private List<CcHospapplyVo> poStuList2VoList(List<CcHospapply> list) {
		if (list == null)
			return null;
		List<CcHospapplyVo> listVo = new ArrayList<>();
		for (CcHospapply hosp : list) {
			CcHospapplyVo hospVo = new CcHospapplyVo();
			// stuVo.setAddress(stu.getAddress());
			// stuVo.setPwd(stu.getPwd());
			hospVo.setApplyid(hosp.getApplyid());
			hospVo.setAdate(hosp.getAdate());
			hospVo.setHaname(hosp.getHaname());// hmedicalid
			hospVo.setHmedicalid(hosp.getHmedicalid());
			hospVo.setHdoctorid(hosp.getHdoctorid());
			hospVo.setHpremarks(hosp.getHpremarks());
			hospVo.setHpsex(hosp.getHpsex());
			hospVo.setHpdate(hosp.getHpdate());
			hospVo.setDepartmentNo(hosp.getHpofficeid().longValue());
			// System.out.println(hospVo.getDepartmentNo());
			// System.out.println(tbdepartDAO.queryName(hospVo.getDepartmentNo()));
			hospVo.setDepartmentName(tbdepartDAO.queryName(hospVo.getDepartmentNo()));
			listVo.add(hospVo);
		}
		return listVo;
	}
	public void addCcHospapply(Date adate,String haname,String hpsex,String hpremarks,BigDecimal hpofficeid,BigDecimal hmedicalid,BigDecimal hdoctorid){
		CcHospapply hospate=new CcHospapply();
		hospate.setAdate(adate);
		hospate.setHaname(haname);
		hospate.setHdoctorid(hdoctorid);
		hospate.setHmedicalid(hmedicalid);
		hospate.setHpofficeid(hpofficeid);
		hospate.setHpremarks(hpremarks);
		hospate.setHpsex(hpsex);
		cchospDAO.save(hospate);
	}
}
